var task1 = function () {
  return new Promise(function (resolve, reject) {
    resolve(100)
  })
}

var task2 = function () {
  return new Promise(function (resolve, reject) {
    resolve(200)
  })
}


function* main() {
  try {
    const a = yield task1();
    console.log(a)

    const b = yield task2();
    console.log(b)

    const c = yield task3();
    console.log(c)
  } catch (error) {
    console.log(error)
  }
}

function co(generator) {
  const g = generator();
  // 实现递归
  function handleResult(result) {
    if (result.done) return;
    result.value.then(
      data => {
        handleResult(g.next(data))
      },
      error => {
        g.throw(error)
      }
    )
  }
  handleResult(g.next())
}

co(main)

